package header

import "edit"
import "panel"
import "keys"
import "edit/buffer"

type HeaderPanel struct {
    edit.EditPanel
    Do panel.Doer
    Other panel.View
}

func NewHeaderPanel(di *panel.DrawInfo, d panel.Doer, other panel.View) *HeaderPanel {
    hp := &HeaderPanel{Do: d, Other: other}
    hp.EditPanel.Draw = di
    hp.EditPanel.SetBuffer( buffer.NewBuffer() )
    return hp
}

func (h *HeaderPanel) Show(message string) {
	// TODO h.EditPanel.Content[0] = message
	panel.Log("Show:", message)
	h.Refresh()
}

func (h *HeaderPanel) FocusForClick(xy panel.Point) panel.View {
    h.Cursor(xy)
    return h
}

func (h *HeaderPanel) Resize(wh panel.Size, offset panel.Point) {
    whx := panel.Size(panel.Point(wh).SubX(20))
    h.EditPanel.Resize(whx, offset.AddX(20)) 
}

func (h *HeaderPanel) Refresh() {
	panel.Log("HeaderPanel.Refresh")
	// content := h.EditPanel.Content
	// if len(content) == 0 { content = []string{""} }
    // h.EditPanel.Refresh()
}

func (h *HeaderPanel) Key(rune int) panel.View {
    switch rune {
    
    case keys.XK_Return:
        command := h.EditPanel.Buffer().Get(h.Line)
        h.EditPanel.Key(rune)
        if h.Do != nil { h.Do.Do(command) }
        return h.Other
        
    case keys.XK_F1:
    	return h.Other
        
    default:
        h.EditPanel.Key(rune)
    }
    return nil
}

